﻿using System;
using Avalonia.Controls;
using FluentAvalonia.UI.Controls;
using iOSClub.Toolkit.WindowApp.ViewModels.Pages;

namespace iOSClub.Toolkit.WindowApp.Views;

class NavigationFactory : INavigationPageFactory
{
    public Control GetPage(Type srcType)
    {
        return null!;
    }
    
    public Control GetPageFromObject(object target)
    {
        if (target is PageModelBase pageModelBase)
        {
            var name = pageModelBase.GetType().FullName!.Split(".")[^1].Replace("Model", "");
            name = $"iOSClub.Toolkit.WindowApp.Pages.{name}";
            var type = Type.GetType(name);
            if (type != null)
            {
                var c = (Control)Activator.CreateInstance(type)!;
                c.DataContext = pageModelBase;
                return c;
            }

            return new TextBlock { Text = "Not Found: " + name };
        }

        if (target is Control control)
        {
            return control;
        }

        throw new Exception();
    }
}